package com.gy.paidao.util;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.mapdemo.R;

public class SelectPicPopupWindow extends PopupWindow {
    private TextView tv_take_photo, tv_pick_photo, tv_cancel;
    private View mMenuView;
    private LinearLayout lv;

    public SelectPicPopupWindow(Activity context, OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.pop_select_picture_layout,
                null);
        tv_take_photo = (TextView) mMenuView
                .findViewById(R.id.pop_select_picture_ll_take);
        tv_pick_photo = (TextView) mMenuView
                .findViewById(R.id.pop_select_picture_ll_pick);
        tv_cancel = (TextView) mMenuView
                .findViewById(R.id.pop_select_picture_ll_cancel);
        lv = (LinearLayout) mMenuView.findViewById(R.id.activity_personal_center_edit_data_select_pic_lv);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.pop_in);
        lv.startAnimation(animation);
        // 取消按钮
        tv_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // 销毁弹出框
                dismiss();
            }

        });
        // 设置按钮监听
        tv_pick_photo.setOnClickListener(itemsOnClick);
        tv_take_photo.setOnClickListener(itemsOnClick);
        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//		// 设置SelectPicPopupWindow弹出窗体动画效果
//		lv.setAnimationStyle(R.style.popupAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.activity_personal_center_edit_data_select_pic_lv)
                        .getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

}
